package com.linkai.onlinetool.controller;

import com.alibaba.fastjson.JSON;
import com.linkai.onlinetool.util.SendMessageByALBB;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class PhoneMessage {

    @GetMapping("/phonnemessage")
//    @CrossOrigin(value = "http://101.132.147.111:8080", maxAge = 1800, allowedHeaders = "*")
    // 处理跨域问题
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    public String sendPhoneMessage(@RequestParam("phoneNumber") String phoneNumber) {

        SendMessageByALBB sendMessageByALBB = new SendMessageByALBB(phoneNumber);
        try {
            sendMessageByALBB.sendMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String randNumberString = String.valueOf(sendMessageByALBB.getRandomNumber());

        // 封装一个 JSON 数据并返回
        Map<String, String> map = new HashMap<>();
        map.put("result", "result");
        map.put("code", randNumberString);
        String resultJSON = JSON.toJSONString(map);
        System.out.println("resultJSON = " + resultJSON);

        return resultJSON;
    }
}
